package com.hgw.Demo06_Singleton.type1;

/**
 * Data time:2021/12/11 13:02
 * StudentID:2019112118
 * Author:hgw
 * Description: 饿汉式（静态常量）
 */
public class SingletonTest01 {
    public static void main(String[] args) {
        // 测试
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1==instance2);
        System.out.println("instance1.hashCode() :" + instance1.hashCode());
        System.out.println("instance2.hashCode() :" + instance2.hashCode());
    }
}

class Singleton {
    // 1、构造器私有化
    private Singleton() {

    }

    // 2、本类内部构建对象实例
    private final static Singleton instance = new Singleton();

    // 3、对外提供一个共有的静态方法 ， 返回实例对象
    public static Singleton getInstance() {
        return instance;
    }
}